﻿Module Module1

    Sub Main()
        Dim str_demo As String = "acb历史def"
        GetStringByIndex(str_demo)
        getContentByIndex(str_demo, -1)
        getContentByIndex(str_demo, 1)
    End Sub

    Sub GetStringByIndex(content As String)
        Console.WriteLine(content + "字符长度" + Len(content).ToString())
        Console.WriteLine(content + "字节长度" + System.Text.Encoding.Default.GetByteCount(content).ToString())
    End Sub


    Sub getContentByIndex(content As String, i_index As Integer)
        Dim i_sum, i_stringSum As Integer
        i_sum = System.Text.Encoding.Default.GetByteCount(content)
        i_stringSum = Len(content)
        Dim str_result As String = content

        Dim str_tmp As String = ""
        Dim str_tmp_priori As String = ""
        Dim i_tmp As Integer = 0
        Dim i_len As Integer = 0

        For index = 1 To i_stringSum
            str_tmp = content.Substring(0, index)
            i_tmp = System.Text.Encoding.Default.GetByteCount(str_tmp)
            i_len = i_tmp
            If i_len > i_index Then
                str_result = str_tmp_priori
                Exit For
            End If
            str_tmp_priori = str_tmp
        Next

        Console.WriteLine(str_result)
    End Sub
End Module
